<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <page-tools>
          <el-button slot="after" type="primary" @click="addPermisson(1,0)">添加权限</el-button>
        </page-tools>

        <el-table v-loading="loading" border row-key="id" :data="list">
          <el-table-column align="center" label="名称" prop="name" />
          <el-table-column align="center" label="标识" prop="code" />
          <el-table-column align="center" label="描述" prop="description" />
          <el-table-column align="center" label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" type="text" @click="addPermisson(2,row.id)">添加</el-button>
              <el-button type="text" @click="showEdit(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>

        </el-table>
      </el-card>
    </div>
    <!-- 放置一个弹层 用来编辑新增节点 -->
    <el-dialog title="新增编辑" :visible.sync="showDialog" @close="reset">
      <!-- 表单 -->
      <el-form ref="perForm" :model="formData" label-width="120px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="isOk">确定</el-button>
          <el-button size="small" @click="showDialog=false">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, delPermission, addPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      list: [],
      loading: false,
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: 0,
        pid: ''// 父级id
      },
      showDialog: false
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.loading = true
      const res = await getPermissionList()
      this.list = tranListToTreeData(res, '0')
      console.log(this.list)
      this.loading = false
    },
    delPermission(id) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.$message.success('删除成功')
        this.getPermissionList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addPermisson(type, pid) {
      this.showDialog = true
      this.formData.pid = pid
      this.formData.type = type
    },
    async isOk() {
      if (this.formData.id) {
        // 编辑
        await updatePermission(this.formData)
        this.$message.success('编辑成功')
      } else {
        // 添加
        await addPermission(this.formData)
        this.$message.success('添加成功')
      }
      this.getPermissionList()
      this.showDialog = false
    },
    reset() {
      this.formData.name = ''
      this.formData.description = ''
      this.formData.enVisible = 0
      this.formData.pid = ''// 父级id
    },
    async showEdit(id) {
      this.showDialog = true
      this.formData = await getPermissionDetail(id)
    }
  }
}
</script>

<style>

</style>
